﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FantastPlatform.Community.IService;
using FantastPlatform.Community.Model;
using FantastPlatform.Community.IMode;
using FantastPlatform.Framework.Service;
using FantastPlatform.Framework;
using NHibernate;
using NHibernate.Criterion;

namespace FantastPlatform.Community.Service
{
    public class BusinessService : DataService<IBusiness, Business>, IBusinessService
    {
        #region IBusinessService 成员
        private PersonService personService = new PersonService();
        private BusinessPersonService businessPersonServie = new BusinessPersonService();
        public void SaveBusiness(IBusiness business)
        {
            if (business.Id == 0)
            {
                foreach (IPerson person in business.Persons)
                {
                    person.CreateDate = DateTime.Now;
                    person.Enable = true;
                    personService.Session.Save(person);
                }
                business.Enable = true;
                business.CreateDate = DateTime.Now;
                base.SaveOrUpdate(business);
            }
            else
            {
                foreach (IPerson person in business.Persons)
                {
                    if (person.Id == 0)
                    {
                        person.CreateDate = DateTime.Now;
                        person.Enable = true;
                        personService.Session.Save(person);
                    }
                    else
                    {
                        person.ModifyDate = DateTime.Now;
                        personService.Session.Update(person);
                    }
                }
                business.Enable = true;
                business.ModifyDate = DateTime.Now;
                base.SaveOrUpdate(business);
            }
        }
        #endregion

        public List<object> GetStreetNumberInfo(string streetName)
        {
            List<object> objList = new List<object>();
            try
            {
                ICriteria criteria = Session.CreateCriteria(typeof(Business));
                criteria.CreateAlias("Street", "s").Add(Expression.Eq("s.Name", streetName)).Add(Expression.Eq("Enable", true));
                IList<IBusiness> businessList = criteria.List<IBusiness>();
                foreach (IBusiness b in businessList)
                {
                    objList.Add(new
                    {
                        Id = b.Id,
                        Adreess = b.Street.Name + b.Address,
                        Name = b.Name,
                        Type = "商铺"
                    });
                }

                criteria = Session.CreateCriteria(typeof(HouseDistrict));
                criteria.CreateAlias("Street", "s").Add(Expression.Eq("s.Name", streetName)).Add(Expression.Eq("Enable", true));
                IList<IHouseDistrict> houseDistrictList = criteria.List<IHouseDistrict>();
                foreach (IHouseDistrict h in houseDistrictList)
                {
                    objList.Add(new
                    {
                        Id = h.Id,
                        Adreess = h.Street.Name + h.Address,
                        Name = h.Name,
                        Type = "住宅"
                    });
                }
            }
            catch (Exception ex)
            {

            }
            return objList;
        }

        public IList<IBusiness> SearchByCondition(string name, string streetName, int policeManId)
        {
            ICriteria criteria = Session.CreateCriteria(typeof(Business));
            criteria.CreateAlias("Street","s").
                Add(Expression.Or(Expression.Like("s.Name", "%" + streetName + "%"),
                Expression.Like("Address", "%" + streetName + "%")))                
                .Add(Expression.Like("Name", "%" + name + "%"))
                .CreateAlias("PoliceMan", "p").Add(Expression.Eq("p.Id", policeManId))
                .Add(Expression.Eq("Enable", true));
            return criteria.List<IBusiness>();
        }

        public IList<IBusiness> GetBusinessByAddress(string address)
        {
            string hql = "select a.* from dbo.Business as a,dbo.Base_QYStreet as b where a.StreetId = b.id and b.Name + a.[address]  = '" + address + "'";

            IQuery query = Session.CreateSQLQuery(hql, "a", typeof(Business));
            IList<IBusiness> list = query.List<IBusiness>();
            return list;
        }

        public override void Update(IBusiness entity)
        {
            ITransaction transaction = null;
            ExecuteTransaction((ITransaction tran) =>
            {

                entity.ModifyDate = DateTime.Now;
                entity.Enable = true;
                Session.Merge(entity);
            }, transaction);
        }
    }
}
